package main

import "math"

func maximumSwap(num int) int {
	max := num
	var parts []int // 从低到高位
	for num > 0 {
		parts = append(parts, num%10)
		num /= 10
	}
	for i := range parts {
		for j := i + 1; j < len(parts); j++ {
			if parts[i] > parts[j] {
				parts[i], parts[j] = parts[j], parts[i]
				n := toInt(parts)
				if n > max {
					max = n
				}
				parts[i], parts[j] = parts[j], parts[i] // recover parts
			}
		}
	}
	return max
}

func toInt(parts []int) int {
	result := 0
	for i, v := range parts {
		result += v * int(math.Pow(10, float64(i)))
	}
	return result
}

func main() {
	println(maximumSwap(2736))
	println(maximumSwap(9973))
	println(maximumSwap(101))
}
